package com.ilomed.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

/**
 * GlobalRepository - test
 * @param <E>
 */
public class GlobalRepository<E> {
	
	@PersistenceContext
	public EntityManager em;
	private final Class<E> entityClass;

	public GlobalRepository(Class<E> entityClass) {
		this.entityClass = entityClass;
	}
	
	@Transactional(readOnly = true)
	public E save(E entity) {
		em.persist(entity);
		
		em.flush();
		
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<E> findAll() {
		return em.createQuery("from " + entityClass.getSimpleName()).getResultList();
	}

}
